//
//  LXDataManager.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/15.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXDataManager.h"

#import "ReceiptAddress+CoreDataProperties.h"


@interface LXDataManager ()
@property(nonatomic,strong)NetRequest * request;
@end

@implementation LXDataManager
+(instancetype)manager{
    
    return [[[self class] alloc] _init];
}

-(id)_init{
    if(self = [super init]){
        self.request = [NetRequest request];
    }
    return self;
}

#pragma mark - 用户注册
-(void)userRegisterWithPrama:(NSDictionary *)prame complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:RegisterAPI prama:prame requestHandle:^(BOOL success, id responseDict) {
        if(success){
            [LXUser upDataUserInfoWith:responseDict[@"data"]];
            if(complete){
                complete(YES,responseDict);
            }
        }else{
            if(complete){
                complete(NO,responseDict);
            }
        }
    }];
}

#pragma mark - 用户登录
-(void)userLoginWithPrama:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:LoginAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            [LXUser upDataUserInfoWith:responseDict[@"data"][@"user"]];
            if(complete)complete(YES,responseDict);
        }else{
           if(complete)complete(NO,responseDict);
        }
    }];
}




-(void)userSocialRegister:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:SocailRegisterAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
             [LXUser upDataUserInfoWith:responseDict[@"data"]];
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)userSocialLogin:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:SocailLoginAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)saveReceptAddress:(NSDictionary *)dict{
    ReceiptAddress *address = [NSEntityDescription insertNewObjectForEntityForName:@"ReceiptAddress" inManagedObjectContext:self.managedObjectContext];
    for (NSString * key in dict.allKeys) {
        [address setValue:dict[key] forKey:key];
    }
    
    [self.managedObjectContext save:nil];
}

-(NSArray*)allAddress{
    NSEntityDescription * entity = [NSEntityDescription entityForName:@"ReceiptAddress" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest * request = [[NSFetchRequest alloc] init];
    
    [request setEntity:entity];
    NSArray * result = [self.managedObjectContext executeFetchRequest:request error:nil];
    return result;
}






#pragma mark - 获取首页banner
-(void)getBannerWithComplete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:BannerAPI prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray * adArray = [NSMutableArray array];
            for (NSDictionary * adDic in responseDict[@"data"][@"banner"]) {
                AdModel * ad = [[AdModel alloc] mj_setKeyValues:adDic];
                [adArray addObject:ad];
            };
            if(complete) complete(YES,adArray);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

-(void)getBannerImageWithAds:(NSArray *)ads complele:(void (^)(BOOL, id))complete{
    __block NSMutableArray * images = [NSMutableArray array];
    __block int downLoadCount = 0;
    for (AdModel * ad in ads) {
        [[NetRequest request] downloadImageWithUrl:ad.pic requestHandle:^(BOOL success, id responseDict) {
            if(success){
                downLoadCount++;
                [images addObject:responseDict];
                if (downLoadCount == ads.count) {
                    if(complete)complete(YES,images);
                }
            }
        }];
    }
}


-(void)getFlashSaleWithComplete:(void (^)(BOOL, id))complete{
    
    [self.request PostWithUrl:FlashsaleAPI prama:@{@"s_name":@"限时抢购"} requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray * productArray = [NSMutableArray array];
            NSDictionary * productDic =responseDict[@"data"];

            for (NSString * key in productDic.allKeys) {
                if(![key isEqualToString:@"nextpageurl"]){
                    NSDictionary * dict = productDic[key];
                Product * product = [[Product alloc] mj_setKeyValues:dict];
                    if(product.product_id)
                        [productArray addObject:product];
                }
            }

            if(complete)complete(YES,productArray);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)getIndexfloorWithComplete:(void (^)(BOOL, id))complete
{
    [self.request GetWithUrl:IndexfloorAPI prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray * advPicModelArray = [NSMutableArray array];
            NSMutableArray * productArray = [NSMutableArray array];
            for (NSDictionary *dict in responseDict[@"data"]) {
                NSMutableArray * subAdv = [NSMutableArray array];
                for (NSDictionary * advDic in dict[@"advpic"]) {
                    AdvPicModel *adv = [[AdvPicModel alloc] mj_setKeyValues:advDic];
                    [subAdv addObject:adv];
                }
                [advPicModelArray addObject:subAdv];
                NSMutableArray * marr = [NSMutableArray array];
                for (NSDictionary * productDic in dict[@"subs"]) {
                    Product * product = [[Product alloc] mj_setKeyValues:productDic];
                    [marr addObject:product];
                }
                
                [productArray addObject:marr];
            }
            if(complete)complete(YES,@[advPicModelArray,productArray]);
            
        }
        else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

-(void)getSpecWithProductID:(NSString *)product_id complete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:ProductSpecAPI prama:@{@"product_id":product_id} requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray * marr = [NSMutableArray array];
            for (NSDictionary * specdDic in responseDict[@"data"]) {
                SpecModel * model = [[SpecModel alloc] mj_setKeyValues:specdDic];
                [marr addObject:model];
            }
            if(complete)complete(YES,marr);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)getProductDetailWithProduct_id:(NSString *)product_id complete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:ProductDetailAPI(product_id) prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            Product * product = [[Product alloc] mj_setKeyValues:responseDict[@"detail"]];
            if(complete)complete(YES,product);
        }
        else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)getCategoryWithComplete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:CategoryAPI prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray * marr = [NSMutableArray array];
            for (NSDictionary * dict in responseDict[@"data"][@"cats"]) {
                CategoryModel * category = [[CategoryModel alloc] mj_setKeyValues:dict];
                [marr addObject:category];
            }
            [[ShareCategory shareCategory] setCategorys:marr];
            if(complete)complete(YES,marr);
        }
        else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)getProductsWithCatt_id:(NSString *)cat_id complete:(void (^)(BOOL, id))complete{
    [self getProductWithPrama:@{@"cat_id":cat_id} complete:complete];
}

-(void)getProductWithPrama:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:GetproductsAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray *marr = [NSMutableArray array];
            NSInteger count = [responseDict[@"data"][@"product"] count];
           __block NSInteger ccccc = 0;
            for (NSDictionary * dict in responseDict[@"data"][@"product"]) {
                Product * product = [[Product alloc] mj_setKeyValues:dict];
                [[LXDataManager manager] getProductDetailWithProduct_id:product.product_id complete:^(BOOL success, id res) {
                    if(success){
                        ccccc++;
                       [marr addObject:res];
                        if(ccccc == count){
                          if(complete)complete(YES,marr);
                        }
                    }
                }];
            }
        }
        else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

-(void)getProductsWithKeyword:(NSString *)keyword complete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:SearchAPI(keyword) prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            ResultModel * res = [[ResultModel alloc] mj_setKeyValues:responseDict[@"data"]];

            if(complete)complete(YES,@[res]);
            
        }
        else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

-(void)getProductsWithUrl:(NSString *)url complete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:url prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            ResultModel * res = [[ResultModel alloc] mj_setKeyValues:responseDict];
            if(complete)complete(YES,@[res]);
        }
        else{
            if(complete)complete(NO,responseDict);
        }
    }];

}


-(void)getsmsCodeWithPhone:(NSString *)phone complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:GetSmsCodeAPI prama:@{@"mobile":phone} requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }
        else{
            if(complete) complete(NO,responseDict);
        }
    }];
}


-(void)upLoadHeaderImage:(UIImage *)image uid:(NSString *)uid complete:(void (^)(BOOL, id))complete{
    NSData * imageData = UIImageJPEGRepresentation(image, 1);
    [self.request PostWithUrl:UpLoadHeaderImageAPI prama:@{@"uid":uid} data:imageData name:@"file" filename:@"user_icon" type:@"image/jpg" requestHandle:^(BOOL success, id responseDict) {
        if(success){
            LXUser * user = [LXUser shareUser];
            user.headerImage = responseDict[@"data"][@"path"];
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

-(void)addShopCartWithProduct:(Product *)product complete:(void (^)(BOOL, id))complete{
    if(LoginState){
        [self.request PostWithUrl:AddToShopCarAPI prama:@{@"user_id":User_id,@"goods_id":product.product_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(complete)complete(YES,responseDict);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
    
}

-(void)delShopCartWithProduct:(Product *)product complete:(void (^)(BOOL, id))complete{
    if(LoginState){
        NSString *urlString = [NSString stringWithFormat:@"%@/%@?cart=%@",DeleteGoodsAPI,User_id,product.product_id];
        [self.request GetWithUrl:urlString prama:nil requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(complete)complete(YES,responseDict);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
   
}

-(void)delAllShopCartWithProduct:(Product *)product complete:(void (^)(BOOL, id))complete{
    if(LoginState){
        [self.request PostWithUrl:AddToShopCarAPI prama:@{@"user_id":User_id,@"num":@"1",@"goods_id":product.product_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                
                NetRequest * request = [NetRequest request];
                NSString *urlString = [NSString stringWithFormat:@"%@/%@?cart=%@",DeleteGoodsAPI,User_id,product.product_id];
                [request GetWithUrl:urlString prama:nil requestHandle:^(BOOL success, id responseDict) {
                    if(success){
                        if(complete)complete(YES,responseDict);
                    }
                    else{
                        if(complete)complete(NO,responseDict);
                    }
                }];
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
    
}

-(void)getShopCartListComplete:(void (^)(BOOL, id))complete{
    
    if(LoginState){
        NSString *urlString = [NSString stringWithFormat:@"%@/%@",ShopCarListAPI,User_id];
        NSLog(@"%s %@",__func__,urlString);
        [self.request GetWithUrl:urlString prama:nil requestHandle:^(BOOL success, id responseDict) {
            if(success){
                NSMutableArray * marr = [NSMutableArray array];
                for (NSDictionary * dict  in responseDict[@"data"][@"cart"]) {
                    Product * p = [[Product alloc] mj_setKeyValues:dict];
                    [marr addObject:p];
                }
                if(complete)complete(YES,marr);
            }else{
                if(complete)complete(NO,responseDict);
            }
            
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
    
}


-(void)getOrderIDWithPrama:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:GenerateOrderAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)getCouponWithKeyWord:(NSString *)word complete:(void (^)(BOOL, id))complete{
    if(LoginState){
        [self.request GetWithUrl:GetCouponAPI prama:@{@"words":word,@"user_id":User_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(complete)complete(YES
                                     ,responseDict);
            }
            else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
    
}

-(void)getGreenBeanWithComplete:(void (^)(BOOL, id))complete{
    if(LoginState){
        [self.request GetWithUrl:GetGreenBeanAPI prama:@{@"user_id":User_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(success){
                    if(complete)complete(YES,responseDict);
                }else{
                    if(complete)complete(NO,responseDict);
                }
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});

    }
    
}

-(void)getUserInfoWithComplete:(void (^)(BOOL, id))complete{
    if(!LoginState){
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }else{
        [self.request GetWithUrl:GetUserInfoAPI prama:@{@"uid":User_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(success){
                    [LXUser upDataUserInfoWith:responseDict[@"data"][@"order"]];
                    [LXUser upDataUserInfoWith:responseDict[@"data"][@"user_info"]];
                    if(complete)complete(YES,responseDict);
                }else{
                    if(complete)complete(NO,responseDict);
                }
            }
        }];
    }
    
}


-(void)getOrderListWithPaynent:(OrderPayment)payment complete:(void (^)(BOOL, id))complete{
    
    if(LoginState){
        NSMutableDictionary * prama = [NSMutableDictionary dictionary];
        [prama setValue:User_id forKey:@"user_id"];
        if(payment!=OrderPaymentAll){
            [prama setValue:@(payment) forKey:@"paystatus"];
        }
        [self.request GetWithUrl:GetOrderListAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
            if(success){
                NSMutableArray * marr = [NSMutableArray array];
                
                for (NSDictionary * dict in responseDict[@"data"][@"data"]) {
                    OrderListModel * model = [[OrderListModel alloc] mj_setKeyValues:dict];
                    [marr addObject:model];
                }
                if(complete)complete(YES,marr);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});

    }
   
}


-(void)getGreenBeanDetailWithComplete:(void (^)(BOOL, id))complete{
    if(LoginState){
        [self.request GetWithUrl:GetGreenBeansDetailAPI prama:@{@"user_id":User_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                NSMutableArray * marr = [NSMutableArray array];
                for (NSDictionary * dict in responseDict[@"data"]) {
                    GreenBeanDetailModel * model = [[GreenBeanDetailModel alloc] mj_setKeyValues:dict];
                    [marr addObject:model];
                }
                if(complete)complete(YES,marr);
            }else{
                if(complete)complete(NO ,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
    
}


-(void)getCouponListWithComplete:(void (^)(BOOL, id))complete{
    if(LoginState){
        [self.request GetWithUrl:GetCouponListAPI prama:@{@"user_id":User_id} requestHandle:^(BOOL success, id responseDict) {
            
            if(success){
                NSMutableArray * marr= [NSMutableArray array];
                for (NSDictionary * dict in responseDict[@"data"]) {
                    CouponModel * model = [[CouponModel alloc] mj_setKeyValues:dict];
                    [marr addObject:model];
                }
                if(complete)complete(YES,marr);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});

    }
    
}


-(void)addAddressWith:(AddressModel *)address complete:(void (^)(BOOL, id))complete{
    if(LoginState){
        NSArray * ps = [[address class] wmproperties];
        NSMutableDictionary * prama = [NSMutableDictionary dictionary];
        [ps enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString * key = obj;
            NSString * value = [address valueForKey:key];
            if(![key isEqualToString:@"addr_id"]){
                [prama setValue:value forKey:key];
            }
        }];
        [prama setValue:[NSString stringWithFormat:@"%@%@%@",address.province,address.city,address.district] forKey:@"addr"];
        [prama setValue:User_id forKey:@"user_id"];
        [self.request PostWithUrl:AddAddressAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(complete)complete(YES,responseDict);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
    
}

-(void)getAddrListWithComplete:(void (^)(BOOL, id))complete{
    
    if(LoginState){
        [self.request GetWithUrl:[NSString stringWithFormat:@"%@/%@",GetAddresListAPI,User_id] prama:nil requestHandle:^(BOOL success, id responseDict) {
            if(success){
                NSMutableArray * marr = [NSMutableArray array];
                for (NSDictionary * dict in responseDict[@"data"][@"addr"]) {
                    AddressModel * model = [[AddressModel alloc] mj_setKeyValues:dict];
                    [marr addObject:model];
                }
                if(complete)complete(YES,marr);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});

    }
    
}

-(void)delAddress:(AddressModel *)address complete:(void (^)(BOOL, id))complete{
    
    if(LoginState){
        if(complete)complete(NO,@{@"error":@"没有登录"});

    }else{
        [self.request GetWithUrl:DelAddresAPI prama:@{@"user_id": User_id,@"addr_id":address.addr_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(complete)complete(YES,complete);
            }else{
                if(complete)complete(NO,complete);
            }
        }];
    }
    
}


-(void)addFocusProduct:(Product *)product complete:(void (^)(BOOL, id))complete{
    
    if(LoginState){
        [self.request PostWithUrl:AddFocusAPI prama:@{@"user_id":User_id,@"product_id":product.product_id} requestHandle:^(BOOL success, id responseDict) {
            NSLog(@"%@",responseDict);
            if(success){
                if(complete)complete(YES,responseDict);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});
    }
  
}

-(void)delFocusProduct:(Product *)product complete:(void (^)(BOOL, id))complete{
    
    if(LoginState){
        [self.request GetWithUrl:DelFocusAPI prama:@{@"user_id":User_id,@"product_id":product.product_id} requestHandle:^(BOOL success, id responseDict) {
            NSLog(@"%@",responseDict);
            if(success){
                if(complete)complete(YES,responseDict);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,@{@"error":@"没有登录"});

    }
    
}


-(void)listFocusProductWithComplete:(void (^)(BOOL, id))complete{
    if(User_id!=nil){
        [self.request GetWithUrl:ListFocusAPI prama:@{@"user_id":User_id} requestHandle:^(BOOL success, id responseDict) {
            if(success){
                NSMutableArray * marr = [NSMutableArray array];
                if(responseDict[@"data"][@"error"]){
                    return ;
                }
                for (NSDictionary * dict in responseDict[@"data"]) {
                    Product * p = [[Product alloc] mj_setKeyValues:dict];
                    [marr addObject:p];
                }
                if(complete)complete(YES,marr);
            }else{
                if(complete)complete(NO,responseDict);
            }
        }];
    }else{
        if(complete)complete(NO,nil);
    }
    
    
}



-(void)getArticleListWithComplete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:GetArticleList prama:nil requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSMutableArray * marr = [NSMutableArray array];
            for (NSDictionary * dict in responseDict[@"data"][@"articles"][@"data"]) {
                ArticleModel * article = [[ArticleModel alloc] mj_setKeyValues:dict];
                [marr addObject:article];
            }
            if(complete)complete(YES,marr);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

-(void)upDataOrderStatuWithPrama:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request  PostWithUrl:UpDataOrderStatuAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSLog(@"参数 = %@\n返回:%@",prama,responseDict);
            if (complete) complete(YES,responseDict);
        }else{
            if (complete) complete(NO,responseDict);
        }
    }];
}


-(void)upDateUserInfoWithPrama:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    NSString * userID = prama[@"user_id"];
    if(userID&&userID.length!=0){
        [self.request PostWithUrl:UpDateUserInfoAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
            if(success){
                if(complete) complete(YES,responseDict);
            }else{
                if(complete) complete(NO,responseDict);
            }
        }];
    }else{
       if(complete) complete(NO,@{@"error":@"用户id为空或不存在"});
    }
}


-(void)forgetPasswordWithPrama:(NSDictionary *)prama complete:(void (^)(BOOL, id))complete{
    [self.request GetWithUrl:ForgetPasswordAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}


-(void)getAliPayInfoWithPrama:(NSDictionary*)prama Complete:(void (^)(BOOL, id))complete{
    [self.request PostWithUrl:GetAliPayInfoAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            NSLog(@"%@",responseDict);
            //MARK:服务器返回数据，可以在这里进行处理 加密或解密
            //MARK:partner  seller_id privateKey
            if(complete)complete(YES,responseDict[@"data"]);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

/**********coreData分割线开始**********/
#pragma mark - Core Data stack

@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"LXMALL" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return _managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }
    
    // Create the coordinator and store
    
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LXMALL.db"];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        // Report any error we got.
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
        dict[NSLocalizedFailureReasonErrorKey] = failureReason;
        dict[NSUnderlyingErrorKey] = error;
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    
    return _persistentStoreCoordinator;
}

- (NSManagedObjectContext *)managedObjectContext {
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (!coordinator) {
        return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    return _managedObjectContext;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        NSError *error = nil;
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}
/**********coreData分割线结束**********/

//..................................我是分割线..............................//
//..................................我是分割线..............................//
//个人中心签到功能

-(void)userSignWithPrama:(NSDictionary*)prama complete:(void(^)(BOOL success,id res))complete
{
    [self.request PostWithUrl:UserSignAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

//获取签到天数
-(void)getSignDaysWithPrama:(NSDictionary*)prama complete:(void(^)(BOOL success,id res))complete
{
    [self.request GetWithUrl:GetSignDaysAPI prama:prama requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
}

//获取用户绿豆数量
-(void)getGreenBeansCountWithPrama:(NSDictionary*)prama complete:(void(^)(BOOL success,id res))complete
{
    if(LoginState){
        LXDataManager * data = [LXDataManager manager];
    [data.request GetWithUrl:GetGreenBeansCountAPI prama:@{@"user_id":User_id} requestHandle:^(BOOL success, id responseDict) {
        if(success){
            if(complete)complete(YES,responseDict);
        }else{
            if(complete)complete(NO,responseDict);
        }
    }];
    }
}

@end
